home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / quarkpy / qquake.py < prev    next >
Text File  |  2004-01-05  |  7KB  |  219 lines

  1. """   QuArK  -  Quake Army Knife
  2.  
  3. Routines to execute Quake, Hexen II, or Quake 2
  4. """
  5. #
  6. # Copyright (C) 1996-99 Armin Rigo
  7. # THIS FILE IS PROTECTED BY THE GNU GENERAL PUBLIC LICENCE
  8. # FOUND IN FILE "COPYING.TXT"
  9. #
  10. #$Header: /cvsroot/quark/runtime/quarkpy/qquake.py,v 1.7 2003/12/17 13:58:59 peter-b Exp $
  11.  
  12.  
  13. import quarkx
  14. from qdictionnary import Strings
  15. import qutils
  16. qutils.loadmapeditor()
  17. from maputils import *
  18. import qconsole
  19.  
  20.  
  21. def BuildConsole():
  22.     return quarkx.setupsubset()["Console"]
  23.  
  24.  
  25. class BatchConsole(qconsole.console):
  26.     "StdOut console for programs that run in batch."
  27.  
  28.     def __init__(self, cmdline, currentdir, next):
  29.         qconsole.console.__init__(self, SILVER)
  30.         self.cmdline = cmdline
  31.         self.currentdir = currentdir
  32.         self.next = next
  33.  
  34.     def close(self):
  35.         try:
  36.             fn = self.next.run
  37.         except:
  38.             return
  39.         del self.next
  40.         fn()
  41.  
  42.     def goon(self, reserved):
  43.         self.close()
  44.  
  45.     def run(self):
  46.         if BuildConsole():
  47.             qconsole.runprogram(self.cmdline, self.currentdir, self)
  48.         else:
  49.             qconsole.runprogram(self.cmdline, self.currentdir).onexit(self.goon)
  50.  
  51.  
  52.  
  53. class GameConsole(BatchConsole):
  54.     "StdOut console to run the game."
  55.  
  56.     DONT_RUN = 0
  57.     NO_MAP = 1
  58.  
  59.     def __init__(self, map, filelist, cfgfile, forcepak, next):
  60.         setup = quarkx.setupsubset()
  61.  
  62.         flst = []
  63.         playerclass = setup["PlayerClass"]
  64.         if playerclass:
  65.             # 'PlayerClass' is specifically only for Hexen-II
  66.             if playerclass != "X":
  67.                 cfgfile = "%splayerclass %s\n" % (cfgfile, playerclass)
  68.             cfg = quarkx.newfileobj("quark.cfg")
  69.             cfg["Data"] = cfgfile
  70.             flst.append(("quark.cfg", cfg))
  71.         else:
  72.             cfgfile = None
  73.         for cf in quarkx.getqctxlist(":", "CreateFiles"):
  74.             for fobj in cf.subitems:
  75.                 flst.append((fobj.name, fobj.copy()))
  76.         for f in filelist:
  77.             flst.append((f, None))
  78.         self.filelistdata = flst
  79.         self.pakfile = quarkx.outputpakfile(forcepak)
  80.  
  81.         dir = setup["Directory"]
  82.         program = setup["Program"]
  83.         if not dir or not program:
  84.             quarkx.openconfigdlg(":")
  85.             raise "Invalid configuration of the game executable"
  86.  
  87.         if map is self.DONT_RUN:
  88.             cmdline = ""
  89.         else:
  90.             format = setup["ExtraCmdLine"]
  91.             customdir = quarkx.outputfile()  # get the current tmpQuArK directory
  92.             if format:
  93.                 cmdline = program + " " + format % customdir
  94.             else:
  95.                 cmdline = program
  96.             if map is not self.NO_MAP:
  97.                 cmdline = cmdline + setup["RunMapCmdLine"] % map
  98.         BatchConsole.__init__(self, cmdline, dir, next)
  99.  
  100.  
  101.     def run(self):
  102.  
  103.         writeto = self.pakfile
  104.         if writeto:
  105.             pak = quarkx.newfileobj(writeto)
  106.             pak["temp"] = "1"
  107.             for qname, qobj in self.filelistdata:
  108.                 nopak = qname[:1]=='*'
  109.                 if nopak:
  110.                     qname = qname[1:]
  111.                 err = ": ready"
  112.                 if qobj is None:
  113.                     try:
  114.                         qobj = quarkx.openfileobj(quarkx.outputfile(qname))
  115.                     except:
  116.                         err = ": ignored"
  117.                 if qobj is not None:
  118.                     if nopak:
  119.                         if quarkx.getfileattr(quarkx.outputfile(qname))>-1: #DECKER - do not overwrite something that already is there!
  120.                             err = ": exists"                                #DECKER
  121.                         else:                                               #DECKER
  122.                             qobj.savefile(quarkx.outputfile(qname))
  123.                     else:
  124.                         type1 = qobj.type.upper()
  125.                         if type1:
  126.                             type2 = qname[-len(type1):].upper()
  127.                             if type1 != type2:
  128.                                 raise "Invalid file types : %s should be of type %s" % (qname,type1)
  129.                             qname = qname[:-len(type1)]
  130.                         i = len(qname)
  131.                         while i and not (qname[i-1] in ("/", "\\")):
  132.                             i = i - 1
  133.                         folder = pak.getfolder(qname[:i])
  134.                         qobj.shortname = qname[i:]
  135.                         folder.appenditem(qobj)
  136.                 print "/" + qname + err
  137.             pak.filename = writeto
  138.             pak.savefile()
  139.         else:
  140.             writeto = quarkx.outputfile("")
  141.             for qname, qobj in self.filelistdata:
  142.                 if qname[:1]=='*':
  143.                     qname = qname[1:]
  144.                 fname = quarkx.outputfile(qname)
  145.                 err = ": ready"
  146.                 if qobj is None:
  147.                     if quarkx.getfileattr(fname)==-1:
  148.                         err = ": ignored"
  149.                 else:
  150.                     if quarkx.getfileattr(fname)>-1:    #DECKER - do not overwrite something that already is there!
  151.                         err = ": exists"                #DECKER
  152.                     else:                               #DECKER
  153.                         qobj.savefile(fname)
  154.                 print "/" + qname + err
  155.         print "Files stored in %s" % writeto
  156.         del self.filelistdata
  157.  
  158.         if not self.cmdline:
  159.             print "Operation finished."
  160.         else:
  161.             #
  162.             # Run Quake !
  163.             #
  164.             oldmapmodes = []
  165.             for p in quarkx.listmapviews():
  166.                 if p.viewmode != "wire":
  167.                     oldmapmodes.append((p, p.viewmode))   # ready to restore the view modes
  168.             self.oldmapmodes = oldmapmodes
  169.  
  170.             formlist = quarkx.forms()
  171.             if len(formlist):
  172.                 try:    # free some memory and closes 3D views
  173.                     formlist[0].macro("3DFR")
  174.                     formlist[0].macro("FREE")
  175.                 except:
  176.                     pass
  177.             del formlist
  178.  
  179.             process = qconsole.runprogram(self.cmdline, self.currentdir, None)   # no console
  180.             process.onexit(self.progexit)
  181.  
  182.  
  183.     def progexit(self, reserved):
  184.         for view, mode in self.oldmapmodes:
  185.             view.viewmode = mode
  186.         self.close()
  187.  
  188.     def close(self):
  189.         BatchConsole.close(self)
  190.         try:
  191.             del self.filelistdata
  192.         except:
  193.             pass
  194.         try:
  195.             del self.oldmapmodes
  196.         except:
  197.             pass
  198.  
  199. # ----------- REVISION HISTORY ------------
  200. #
  201. #
  202. #$Log: qquake.py,v $
  203. #Revision 1.7  2003/12/17 13:58:59  peter-b
  204. #- Rewrote defines for setting Python version
  205. #- Removed back-compatibility with Python 1.5
  206. #- Removed reliance on external string library from Python scripts
  207. #
  208. #Revision 1.6  2001/07/08 20:56:30  tiglari
  209. #fix crash when ExtraCMDLine=""
  210. #
  211. #Revision 1.5  2001/06/21 17:34:12  decker_dk
  212. #quarkx.openconfigdlg()
  213. #
  214. #Revision 1.4  2000/06/02 16:00:22  alexander
  215. #added cvs headers
  216. #
  217. #
  218. #
  219.